package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter{

    private Group<Message> mMsgs;
    private LayoutInflater mInflater;
    private ButtonRowClickHandler mClickListener;

    public MessageAdapter( Context context, Group<Message> msgs, ButtonRowClickHandler clickListener ) {
        mMsgs = msgs;
        mInflater = LayoutInflater.from(context);
        mClickListener = clickListener;
    }

    public void setList(Group<Message> msgs){
        mMsgs = msgs;
    }

    @Override
    public int getCount() {
        return mMsgs.size();
    }

    @Override
    public Message getItem(int position) {
        return mMsgs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
             convertView = mInflater.inflate(R.layout.message_list_item, null);
             holder = new ViewHolder();
             holder.photo = (ImageView) convertView.findViewById(R.id.imgMessagePhoto);
             holder.name = (TextView) convertView.findViewById(R.id.tvMessageName);
             holder.title = (TextView) convertView.findViewById(R.id.tvMessageTitle);
             holder.time = (TextView) convertView.findViewById(R.id.tvMessageTime);
             holder.delete = (Button) convertView.findViewById(R.id.btnMessageDelete);
             holder.clickable = (RelativeLayout) convertView.findViewById(R.id.msgListItemClickableArea);
             convertView.setTag(holder);
             holder.clickable.setOnClickListener(mOnClickListenerInfo);
             holder.delete.setOnClickListener(mOnClickListenerDelete);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }
                
        Message msg = mMsgs.get(position);
         if (msg.getIsReaded()) {
             holder.photo.setImageResource(R.drawable.msg_read);
         }  else {
             holder.photo.setImageResource(R.drawable.msg_unread);
         }
         holder.name.setText(msg.getUserName());
         holder.title.setText(msg.getTitle());
         holder.time.setText(msg.getDate());
         holder.delete.setTag(new Integer(position));
         return convertView;
    }

    private OnClickListener mOnClickListenerInfo = new OnClickListener() {
        @Override
        public void onClick(View v) {          
            ViewHolder obj = (ViewHolder) v.getTag();
            mClickListener.onBtnClickInfo((Integer) obj.delete.getTag());
        }
    };

    private OnClickListener mOnClickListenerDelete = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer index = (Integer) v.getTag();
            mClickListener.onBtnClickDelete(index);
        }
    };

    static class ViewHolder {
        RelativeLayout clickable;
        ImageView photo;
        TextView name;
        TextView title;
        TextView time;
        Button delete;
    }

    public interface ButtonRowClickHandler {
        public void onBtnClickDelete(int position);
        public void onBtnClickInfo(int position);
    }
}
